package com.ltwc.lttv.tools

import android.content.Context
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

/**
 * Description：
 * Created on 2024/2/1 21:43
 * Organization:华云
 * author：AHuangSHang
 **/
class SaveDataTool {
    companion object instance {
        fun saveListToPrefs(context: Context, listName: String, dataList: List<Any>) {
            val sharedPreferences = context.getSharedPreferences("MyApp", Context.MODE_PRIVATE)
            val editor = sharedPreferences.edit()

            // 使用Gson将List转换为JSON字符串
            val gson = Gson()
            val jsonString = gson.toJson(dataList)

            // 将JSON字符串保存到SharedPreferences中
            editor.putString(listName, jsonString).apply()
        }

        // 从SharedPreferences读取并还原List
        fun getListFromPrefs(context: Context, listName: String): String {
            val sharedPreferences = context.getSharedPreferences("MyApp", Context.MODE_PRIVATE)
            val jsonString = sharedPreferences.getString(listName, "") ?: return "null"
            return jsonString;
            /*// 使用Gson将JSON字符串转换为List
            val gson = Gson()
            val typeToken = object : TypeToken<List<Any>>() {}.type
            return gson.fromJson(jsonString, typeToken)*/
        }
    }
}